import request from '/@/utils/request'
import { AxiosResponse } from 'axios'

const api = {
    searchFund: "/search/fund",
    filterFund: "/filter/fund",
    fundDetail: "/fund/detail",
    fundJzPic: "/fund/jz/pic",
    fundJzgsPic: "/fund/jzgs/pic",
    fundJS: "/fund/js",
    fundLjsylzs: "fund/ljsylzs",
    fundJbxx: "fund/jbxx",
    fundYjpjbj: "fund/yjpjbj",
    fundLjsyl: "fund/ljsyl",
}

export interface IListRes<T = any> {
    datas: T[]
    currentPage: number
    pageSize: number
    total: number
}

interface ListParam {
    currentPage: number
    pageSize: number
    total?: number
}

export interface SearchFundRes {
    CODE: string
    CATEGORY: number
    NAME: string
}


export function searchFund(param: { keyword: string }): Promise<AxiosResponse<IResponse<SearchFundRes[]>>> {
    return request({
        url: api.searchFund,
        method: 'get',
        params: param
    })
}

// export const TPS = { "BK0429": "交运设备", "BK0433": "农牧饲渔", "BK0438": "食品饮料", "BK0447": "互联网服务", "BK0448": "通信设备", "BK0451": "房地产开发", "BK0454": "塑料制品", "BK0456": "家电行业", "BK0459": "电子元件", "BK0465": "化学制药", "BK0473": "证券", "BK0474": "保险", "BK0475": "银行", "BK0478": "有色金属", "BK0479": "钢铁行业", "BK0480": "航天航空", "BK0481": "汽车零部件", "BK0490": "军工", "BK0492": "煤化工", "BK0493": "新能源", "BK0494": "节能环保", "BK0509": "网络游戏", "BK0512": "化工原料", "BK0519": "稀缺资源", "BK0538": "化学制品", "BK0545": "通用设备", "BK0547": "黄金概念", "BK0548": "生物疫苗", "BK0552": "机构重仓", "BK0554": "物联网", "BK0561": "基本金属", "BK0571": "预盈预增", "BK0574": "锂电池", "BK0579": "云计算", "BK0588": "太阳能", "BK0592": "铁路基建", "BK0597": "水利建设", "BK0600": "参股新三板", "BK0615": "中药概念", "BK0629": "北斗导航", "BK0634": "大数据", "BK0638": "创业成份", "BK0641": "智能穿戴", "BK0642": "手游概念", "BK0644": "特斯拉", "BK0655": "网络安全", "BK0666": "苹果概念", "BK0667": "国家安防", "BK0668": "医疗器械概念", "BK0683": "国企改革", "BK0693": "基因测序", "BK0695": "小金属概念", "BK0696": "国产软件", "BK0700": "充电桩", "BK0711": "券商概念", "BK0714": "5G概念", "BK0727": "医疗服务", "BK0728": "环保行业", "BK0731": "化肥行业", "BK0732": "贵金属", "BK0735": "计算机设备", "BK0737": "软件开发", "BK0739": "工程机械", "BK0800": "人工智能", "BK0806": "精准医疗", "BK0808": "军民融合", "BK0814": "大飞机", "BK0821": "MSCI中国", "BK0823": "养老金", "BK0830": "区块链", "BK0832": "工业互联", "BK0835": "独角兽", "BK0840": "OLED", "BK0852": "冷链物流", "BK0853": "电子竞技", "BK0854": "华为概念", "BK0884": "光刻胶", "BK0891": "国产芯片", "BK0893": "无线耳机", "BK0896": "白酒", "BK0899": "CRO", "BK0900": "新能源车", "BK0903": "云游戏", "BK0905": "传感器", "BK0910": "专用设备", "BK0916": "氮化镓", "BK0917": "半导体概念", "BK0922": "数据中心", "BK0952": "第三代半导体", "BK0960": "无线充电", "BK0963": "航天概念", "BK0966": "碳交易", "BK0968": "固态电池", "BK0969": "汽车芯片", "BK0976": "被动元件", "BK0977": "碳化硅" }
export const TPS = { 
"801104": "软件开发",
"801741": "航天装备",
"861060": "云计算",
"801103": "IT服务",
"801750": "计算机",
"801770": "通信",
"801102": "通信设备",
"801733": "其他电源设备",
"861183": "5G概念",
"861258": "华为概念",
"801760": "传媒",
"861399": "元宇宙概念",
"861331": "中芯概念",
"861205": "国产芯片",
"861045": "物联网",
"861013": "中字头",
"861169": "充电桩",
"861380": "储能",
"861001": "军工",
"861120": "特斯拉",
"861040": "深圳特区",
"801991": "航空机场",
"861206": "新能源车",
"861220": "军民融合",
"861153": "国企改革",
"861137": "电商概念",
"861034": "QFII重仓",
"861251": "影视概念",
"861053": "预亏预减",
"801083": "元件",
"801125": "白酒",
"801072": "通用设备",
"801053": "贵金属",
"861043": "机构重仓",
"801890": "机械设备",
"861434": "跨境支付",
"861271": "富时罗素",
"861282": "标准普尔",
"861222": "贬值受益",
"861004": "新能源",
"861038": "黄金概念",
"861089": "央视50",
"861075": "风能",
"861187": "证金持股",
"861174": "上证380",
"861076": "融资融券",
"801120": "食品饮料",
"861032": "转债标的",
"861216": "深股通",
"861008": "AH股",
"861028": "新材料",
"861170": "中证500",
"801980": "美容护理",
"861176": "沪股通",
"861221": "超级品牌",
"861224": "大飞机",
"801085": "消费电子",
"861054": "预盈预增",
"861366": "化妆品概念",
"801110": "家用电器",
"861102": "通用航空",
"801074": "专用设备",
"861113": "互联金融",
"801880": "汽车",
"801093": "汽车零部件",
"861051": "深成500",
"861024": "稀缺资源",
"801723": "基础建设",
"861389": "茅指数",
"861009": "HS300",
"801740": "国防军工",
"861122": "养老概念",
"861057": "锂电池",
"861240": "乡村振兴",
"861204": "猪肉概念",
"861035": "基金重仓",
"861094": "中药概念",
"861290": "医疗美容",
"861230": "MSCI中国",
"801095": "乘用车",
"801720": "建筑装饰",
"801170": "交通运输",
"801155": "中药",
"861091": "上证180",
"801080": "电子",
"861289": "农业种植",
"801200": "商业贸易",
"801111": "白色家电",
"801160": "公用事业",
"861305": "流感",
"861361": "固态电池",
"861378": "盐湖提锂",
"861180": "券商概念",
"861114": "创业成份",
"801194": "保险",
"801735": "光伏设备",
"801790": "非银金融",
"801034": "化学制品",
"801193": "证券",
"861090": "上证50",
"801151": "化学制药",
"861306": "转基因",
"801742": "航空装备",
"861422": "百元股",
"801054": "小金属",
"861003": "煤化工",
"801950": "煤炭",
"801081": "半导体",
"801030": "基础化工",
"801050": "有色金属",
"801970": "环保",
"861390": "宁组合",
"801010": "农林牧渔",
"801150": "医药生物",
"801153": "医疗器械",
"801713": "装修建材",
"801730": "电力设备",
"801033": "化学原料",
"801040": "钢铁",
"801710": "建筑材料",
"801780": "银行",
"861299": "CRO",
"801156": "医疗服务",
"801152": "生物制品",
"801056": "能源金属",
"801181": "房地产开发",
"801180": "房地产",
"801737": "电池",
}

export const FTS = {
    gp: "股票型",
    hh: "混合型",
    zq: "债券型",
    zs: "指数型",
    qdii: "QDII",
    fof: "FOF"
}

export const RsUnit = {
    "z": "近1周",
    "1y": "近1月",
    "3y": "近3月",
    "6y": "近6月",
    "jn": "今年来",
    "1n": "近1年",
    "2n": "近2年",
    "3n": "近3年",
}

export const RsRank = {
    10: "前10名",
    20: "前20名",
    50: "前50名",
    100: "前100名",
}

export interface FilterFundParam extends ListParam {
    ft?: any // 基金类型
    rs?: any // 基金业绩
    tp?: any // 基金主题
}

export interface FilterFundRes {
    "code": string
    "name": string
    "jjjp": string
    "type": string
    "yt": string
    "w1": string
    "m1": string
    "m3": string
    "m6": string
    "y1": string
    "y2": string
    "y3": string
    "12_": string
    "13_": string
    "14_": string
    "date": string
    "netv": string
    "rate": string
    "18_": string
    "charge": string
    "min": string
    "charge2": string
    "22_": string
    "fn": string
}

export function filterFund(param: FilterFundParam): Promise<AxiosResponse<IResponse<IListRes<FilterFundRes>>>> {
    return request({
        url: api.filterFund,
        method: 'get',
        params: param
    })
}

export interface FundDetailRes {
    "fundcode": string
    "name": string
    "jzrq": string
    "dwjz": string
    "gsz": string | null
    "gszzl": string | null
    "gztime": string | null
    "ljjz": string
    "m1": string
    "m3": string
    "m6": string
    "y1": string
    "y3": string
    "fn": string
    "type": string
    "scale": string
    "manager": string
    "fn_date": string
    "company": string
    stock10: {
        data: Array<[string, string, string]>
        end_date: string
        sum: string
    }
    bond10: {
        data: Array<[string, string]>
        end_date: string
        sum: string
    }
    jzhistory: Array<[string, string, string, string]>
}

export function fundDetail(code: string): Promise<AxiosResponse<IResponse<FundDetailRes>>> {
    return request({
        url: api.fundDetail,
        method: 'get',
        params: { code }
    })
}

export function fundJzPic(code: string): string {
    const baseURL = request.defaults.baseURL
    return baseURL + `${api.fundJzPic}?code=${code}`
}

export function fundJzgsPic(code: string): string {
    const baseURL = request.defaults.baseURL
    return baseURL + `${api.fundJzgsPic}?code=${code}`
}

export function fundJS(code: string): Promise<AxiosResponse<any>> {
    return request({
        url: api.fundJS,
        method: 'get',
        params: { code },
        responseType: 'text',
    })
}

export interface FundLjsylzsRes {
    data: Array<[number, number]>
    name: string
}

export const Types = {
    m1: '1月',
    m3: '3月',
    m6: '6月',
    y1: '1年',
    y3: '3年',
    y5: '5年',
    yt: '今年',
    max: '最大'
}

export function fundLjsylzs(code: string, type: keyof typeof Types = 'm6'): Promise<AxiosResponse<IResponse<FundLjsylzsRes[]>>> {
    return request({
        url: api.fundLjsylzs,
        method: 'get',
        params: { code, type },
    })
}

export interface fundJbxxRes {
    "code": string
    "name": string
    "jjjp": string  // 简拼
    "type": string
    "pzgz": string  // 盘中估值
    "gzzf": string  // 估值涨幅
    "jz": string  // 最新单位净值
    "jzrq": string  // 净值日期
    "ljjz": string  // 累计净值
    "rzzl": string  // 日增长率
    "sqdwjz": string  // 上期单位净值
    "sqjzrq": string  // 上期单位净值
    "manager": string  // 基金经理
    "13_": string
    "company": string  // 基金公司
    "15_": string
}

export function fundJbxx(code: string): Promise<AxiosResponse<IResponse<fundJbxxRes[]>>> {
    return request({
        url: api.fundJbxx,
        method: 'get',
        params: { code },
    })
}


export interface FundYjpjbjRes {
    "code": string
    "name": string
    "date": string  // 成立日期
    "yt": string  // 今年来
    "w1": string  // 近一周
    "m1": string  // 近一月
    "m3": string
    "m6": string
    "y1": string  // 近一年
    "y2": string
    "y3": string
    "y5": string
    "fn": string  // 成立以来
}

export function fundYjpjbj(code: string): Promise<AxiosResponse<IResponse<FundYjpjbjRes[]>>> {
    return request({
        url: api.fundYjpjbj,
        method: 'get',
        params: { code },
    })
}

export const Types2 = {
    "week": "近1周",
    "month": "近1月",
    "threemonth": "近3月",
    "sixmonth": "近6月",
    "year": "近1年",
    "twoyear": "近2年",
    "threeyear": "近3年",
}

export interface FundLjsylRes {
    graph: Array<{
        name: string
        valueField: string
    }>
    dataProvider: Array<{
        date: string
        [key: string]: any
    }>
}

export function fundLjsyl(code: string, c: keyof typeof Types2 = "month"): Promise<AxiosResponse<IResponse<FundLjsylRes>>> {
    return request({
        url: api.fundLjsyl,
        method: 'get',
        params: { code, c },
    })
}